package learn.Thread;

import java.util.Stack;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadPrintABCReentranLock {
    public static void main(String[] args) {
        Thread a = new ThreadPrint2(0,"A");
        Thread b = new ThreadPrint2(1,"B");
        Thread c = new ThreadPrint2(2,"C");
        a.start();
        b.start();
        c.start();
        Stack<Character> stack = new Stack<>();
    }
}

class ThreadPrint2 extends Thread{
    private static ReentrantLock lock = new ReentrantLock();
    private static Condition condition = lock.newCondition();

    private static int count = 0;
    private String value;
    private int flag;
    public ThreadPrint2 (int flag,String value){
        this.value = value;
        this.flag = flag;
    }

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            lock.lock();
            while(count%3!=flag){
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(value);
            count++;
            condition.signalAll();
            lock.unlock();
        }
    }
}
